/**
 *
 * @author Jaime Moreno 
 * Developed as a team based project for a course at the California State University of Monterey Bay by Reynold Rondez, Jaime Moreno, Chris Sterling, Eric McDonald
 *  SimpleLoop main program example using Java 2D for start of project for teambowser
 * just a line test to see how editing in browser works
 * just adding another test line
 * Test of moving sprite using keyboard input - goal for next week.
 * Use keys to move block around screen. Use R, G, B, W to change color of background.
 */


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*; // for Java 2D stuff
import java.applet.*;
import java.util.*;

public class DungeonMaster extends Applet implements Runnable,KeyListener, MouseListener   {

	 /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	//random number generator
    Random rand = new Random();

    //the main thread
    Thread thread;

    //count the number of rectangles drawn
    long count = 0;
    
 // moveable black rectangle 

    private Rectangle r; 
    
  //here's the shape used for drawing
    private Shape shape;

    // current background color for the applet 

    private Color backColor; 

    int x = 0; // movement in x direction
    int y = 0; // movement in y direction
    
    // this method overrides the init method from the Applet class 
    //applet init event since an applet uses init instead of main like a regular java program
    public void init() {
        r = new Rectangle(0, 0, 20, 10); 
        shape = new Rectangle2D.Double(-1.0, -1.0, 20.0, 10.0);

        backColor = Color.WHITE; 

        addKeyListener(this); 
    }

    //applet start event
    public void start() {
        thread = new Thread(this);
        thread.start();
    }

    //thread run event
    public void run() {
        long start = System.currentTimeMillis();

        //acquire the current thread
        Thread current = Thread.currentThread();

        //here's the new game loop
        while (current == thread) {
            try {
                Thread.sleep(0);
            }
            catch(InterruptedException e) {
                e.printStackTrace();
            }

            //draw something
            repaint();

            //figure out how fast it's running
            if (start + 1000 < System.currentTimeMillis()) {
                start = System.currentTimeMillis();
                showStatus("Rectangles per second: " + count);
                count = 0;
            }
        }
    }

    //applet stop event
    public void stop() {
        thread = null;
    }

    //applet update event
    public void update(Graphics g) {
        paint(g);
    }

    //applet paint event
    public void paint(Graphics g) {
       
     // paints the rectangle at the updated position 
     // not using java2d then using java2d
       /*  g.setColor(backColor); // set background color
        g.fillRect(0,0,this.getWidth(), this.getHeight());

        g.setColor(Color.BLACK); // set block to black
        g.fillRect(r.x, r.y, r.width, r.height); 
        */
    	
    	 //create an instance of Graphics2D
        Graphics2D g2d = (Graphics2D)g;

        //save the identity transform
        AffineTransform identity = new AffineTransform();


        //save the window width/height
        int width = getSize().width;
        int height = getSize().height;

        //fill the background with black
        g2d.setColor(Color.BLACK);
        g2d.fillRect(0, 0, width, height);

        //reset Graphics2D to the identity transform
        g2d.setTransform(identity);

        //move, rotate, and scale the shape according to key user pressed
        g2d.translate(x, y);


        //draw the shape with a red color
        g2d.setColor(Color.RED);
        g2d.fill(shape);

    }

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
	     // for this method, use the key code to generate movement 

	     int keyCode = e.getKeyCode(); 

	     // move the rectangle 

	     if(keyCode == KeyEvent.VK_LEFT) 

	     { 

	          x -= 5; 

	          if(x < 0) x = 0; // minimal checking to make sure block doesn't go off left side of screen

	          repaint(); 

	     } 
	     
	     else if(keyCode == KeyEvent.VK_RIGHT) 

	     { 

	          x += 5; 

	          if(x > getSize().width-r.width)  // make sure we don't go off to the right

	          { 

	               x = getSize().width-r.width; 

	          } 

	          repaint(); 

	     } 

	     else if(keyCode == KeyEvent.VK_UP) 

	     { 

	          y -= 5; 

	          if(y < 0) y = 0;   // can't go off top

	          repaint(); 

	     } 

	     else if(keyCode == KeyEvent.VK_DOWN) 

	     { 

	          y += 5; 

	          if(y > getSize().height-r.height) // can't go off bottom

	          { 

	               y = getSize().height-r.height; 

	          } 

	          repaint(); 

	     } 
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		 // for this method, use the actual key char to call action 

	     char keyChar = e.getKeyChar(); 

	     // change the background color 

	     switch(keyChar) 

	     {

			case 'r': 
		
			{ 
		
			      backColor = Color.RED; 
		
			      repaint(); 
		
			      break; 
		
			} 
		
			case 'g': 
		
			{ 
		
			      backColor = Color.GREEN; 
		
			      repaint(); 
		
			      break; 
		
			} 
		
			case 'b': 
		
			{ 
		
			      backColor = Color.BLUE; 
		
			      repaint(); 
		
			      break; 
		
			} 
		
			case 'w': 
		
			{ 
		
			      backColor = Color.WHITE; 
		
			      repaint(); 
		
			      break; 
		
			} 
	     } // end switch
		
	}

}










 



